package com.hong.config;

import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Configuration
@EnableOpenApi
public class SwaggerConfig {

    public static final String TOKEN_HEADER = "Authorization";


    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(true) //配置是否启用Swagger，如果是false，在浏览器将无法访问
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                ////设置全局token
                .securitySchemes(security())
                .securityContexts(securityContexts());
                //每个接口传token
//                .globalRequestParameters(globalRequestParameters());
    }

    //配置文档信息
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("mybatis-plus 测试系统")
                .description("swagger3.0接口文档")
                .contact(new Contact("Tom","",""))
                .version("3.0")
                .license("")
                .build();
    }


    private List<SecurityScheme> security() {
        List<SecurityScheme> apiKeys = new ArrayList<>();
        ApiKey header = new ApiKey(TOKEN_HEADER, TOKEN_HEADER, In.HEADER.toValue());
        apiKeys.add(header);

        return apiKeys;
    }
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(
                SecurityContext
                        .builder()
                        .securityReferences(securityReferences())
                        .operationSelector(operationContext ->operationContext.requestMappingPattern().startsWith("/"))
                        .build());
        return securityContexts;
    }

    private List<SecurityReference> securityReferences() {
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[] {new AuthorizationScope("global", "accessEverything")};
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference(TOKEN_HEADER, authorizationScopes));
        return securityReferences;
    }

    /**
     * 每个接口传token
     * @return
     */
    private List<RequestParameter> globalRequestParameters() {
        return Arrays.asList(new RequestParameterBuilder()
                .name(TOKEN_HEADER)
                .description("access token")
                .in(ParameterType.HEADER)
                .required(false)
                .build());
    }




}
